package com.party.service.sys.impl;

import com.party.common.Page;
import com.party.mapper.SysModelMapper;
import com.party.mapper.SysNavMapper;
import com.party.mapper.SysNodeMapper;
import com.party.model.*;
import com.party.parBean.Par_sysUser;
import com.party.service.sys.MenuService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-31
 * Time: 下午3:15
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class MenuServiceImpl implements MenuService {
    @Resource
    SysNodeMapper sysNodeMapper;
    @Resource
    SysModelMapper sysModelMapper;
    @Resource
    SysNavMapper sysNavMapper;

    public Page getModelList(SysModel sysModel, int pageSize, int pageNumber) {
        SysModelExample sysModelExample = new SysModelExample();
        if(sysModel != null)
        {
            SysModelExample.Criteria criteria = sysModelExample.createCriteria();
            if(sysModel.getName() != null && !"".equals(sysModel.getName()))
            {
                criteria.andNameLike("%"+sysModel.getName()+"%");
            }
        }
        int count = this.sysModelMapper.countByExample(sysModelExample);
        sysModelExample.setBeginNum((pageNumber - 1) * pageSize);
        sysModelExample.setEndNum(pageSize);
        List<SysModel> list = this.sysModelMapper.getListPage(sysModelExample);
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;
    }

    public Page getNavList(SysNav sysNav, int pageSize, int pageNumber) {
        SysNavExample sysNavExample = new SysNavExample();

        if(sysNav != null)
        {
            SysNavExample.Criteria criteria = sysNavExample.createCriteria();
            if(sysNav.getName() != null && !"".equals(sysNav.getName()))
            {
                criteria.andNameLike("%"+sysNav.getName()+"%");
            }
        }

        int count = this.sysNavMapper.countByExample(sysNavExample);
        sysNavExample.setBeginNum((pageNumber - 1) * pageSize);
        sysNavExample.setEndNum(pageSize);
        List<SysNav> list = this.sysNavMapper.getListPage(sysNavExample);
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;
    }

    public Page getNodeList(SysNode sysNode, int pageSize, int pageNumber) {
        SysNodeExample sysNodeExample = new SysNodeExample();
        if(sysNode != null)
        {
            SysNodeExample.Criteria criteria = sysNodeExample.createCriteria();
            if(sysNode.getName() != null && !"".equals(sysNode.getName()))
            {
                criteria.andNameLike("%"+sysNode.getName()+"%");
            }
            if(sysNode.getModelid() != null)
            {
                criteria.andModelidEqualTo(sysNode.getModelid());
            }
            if(sysNode.getCode()!= null && !"".equals(sysNode.getCode()))
            {
                criteria.andCodeLike("%"+sysNode.getCode()+"%");
            }
        }
        int count = this.sysNodeMapper.countByExample(sysNodeExample);
        sysNodeExample.setBeginNum((pageNumber - 1) * pageSize);
        sysNodeExample.setEndNum(pageSize);
        List<SysNode> list = this.sysNodeMapper.getListPage(sysNodeExample);
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;
    }

    public int addNav(SysNav sysNav) {
        return sysNavMapper.insertSelective(sysNav);
    }

    public int addModel(SysModel sysModel) {
        return sysModelMapper.insertSelective(sysModel);
    }

    public int addNode(SysNode sysNode) {
        return sysNodeMapper.insertSelective(sysNode);
    }

    public int delNav(String ids) {
        int num = 0;
        if (ids != null) {
            String[] nodeids = ids.split(",");
            for (String id : nodeids) {
                num += sysNavMapper.deleteByPrimaryKey(Integer.parseInt(id));
            }
        }
        return num;
    }

    public int delModel(String ids) {
        int num = 0;
        if (ids != null) {
            String[] nodeids = ids.split(",");
            for (String id : nodeids) {
                num += sysModelMapper.deleteByPrimaryKey(Integer.parseInt(id));
            }
        }
        return num;
    }

    public int delNode(String ids) {
        int num = 0;
        if (ids != null) {
            String[] nodeids = ids.split(",");
            for (String id : nodeids) {
                num += sysNodeMapper.deleteByPrimaryKey(Integer.parseInt(id));
            }
        }
        return num;
    }

    public int updateNav(SysNav sysNav) {
        int num = sysNavMapper.updateByPrimaryKey(sysNav);
        return num;
    }

    public int updateModel(SysModel sysModel) {
        return sysModelMapper.updateByPrimaryKey(sysModel);
    }

    public int updateNode(SysNode sysNode) {
        return sysNodeMapper.updateByPrimaryKey(sysNode);
    }

    public List<SysNav> getNavsByPassport(String passport) {
        return sysNavMapper.getNavsByPassport(passport);
    }

    public List<SysModel> getModelsByPassport(String passport, int navid) {
        Par_sysUser par_sysUser = new Par_sysUser();
        par_sysUser.setNavid(navid);
        par_sysUser.setPassport(passport);
        return sysNavMapper.getModelsByPassport(par_sysUser);
    }

    public List<SysNode> getNodesByPassport(String passport, int modelid) {
        Par_sysUser par_sysUser = new Par_sysUser();
        par_sysUser.setPassport(passport);
        par_sysUser.setModelid(modelid);
        return sysNavMapper.getNodesByPassport(par_sysUser);
    }

    public List<SysNav> getNavs() {
        return sysNavMapper.selectByExample(new SysNavExample());  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<SysModel> getModels(SysModel sysModel) {
        SysModelExample sysModelExample = new SysModelExample();
        SysModelExample.Criteria criteria = sysModelExample.createCriteria();
        if (sysModel.getNavid() != null) {
            criteria.andNavidEqualTo(sysModel.getNavid());
        }
        List<SysModel> sysModels = sysModelMapper.selectByExample(sysModelExample);
        return sysModels;
    }

    public String getNodesStr() {

        StringBuilder nodeList = new StringBuilder();
        List<SysNav> sysNavs = sysNavMapper.selectByExample(new SysNavExample());
        for (SysNav sysNav : sysNavs) {
            nodeList.append("<tr><th width=\"180\"><input id=\"" + sysNav.getNavid() + "\" type=\"checkbox\" name=\"navname\" onclick=\"deal(this.id);\" >" + sysNav.getName() + "</th><td colspan=\"2\"></td></tr>");

            SysModelExample sysModelExample = new SysModelExample();
            SysModelExample.Criteria criteria = sysModelExample.createCriteria();
            criteria.andNavidEqualTo(sysNav.getNavid());

            List<SysModel> sysModels = sysModelMapper.selectByExample(sysModelExample);
            for (SysModel sysModel : sysModels) {
                nodeList.append("<tr class=\"nav_" + sysNav.getNavid() + "\"><th width=\"180\"></th><th style=\"width:50px;\"><input id=\"node_" + sysModel.getModelid() + "\" type=\"checkbox\" name=\"model\" value=\"" + sysModel.getModelid() + "\" onclick=\"deal2(this);\">" + sysModel.getName() + "</th><td>");

                SysNodeExample sysNodeExample = new SysNodeExample();
                SysNodeExample.Criteria criteriaxx = sysNodeExample.createCriteria();
                criteriaxx.andModelidEqualTo(sysModel.getModelid());

                List<SysNode> sysNodes = sysNodeMapper.selectByExample(sysNodeExample);
                for (SysNode sysNode : sysNodes) {
                    nodeList.append("<input id=\"node_" + sysNode.getNodeid() + "\" type=\"checkbox\" name=\"node\" value=\"" + sysNode.getNodeid() + "\">" + sysNode.getName() + "");
                }
                nodeList.append("</td></tr>");
            }
        }
        return nodeList.toString();
    }


    public SysNav getNav(int navid) {
        SysNav tNavs = sysNavMapper.selectByPrimaryKey(navid);
        return tNavs;
    }

    public SysModel getModel(int modelid){
        return sysModelMapper.selectByPrimaryKey(modelid);
    }

    public SysNode getNode(int nodeid){
        return sysNodeMapper.selectByPrimaryKey(nodeid);
    }
}
